package com.kanche.bigbaby.collection.ui;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.kanche.bigbaby.R;
import com.kanche.bigbaby.collection.StringUtils;

//import com.kkche.bss.R;
//import com.kkche.bss.utils.StringUtils;

/**
 * Created by nealmi on 6/11/14.
 */
public class ItemEditTextWithActionButtonConfig extends ItemEditTextConfig implements View.OnClickListener {
    public static final int PREFIX_ACTION_BUTTON = 1;
    public static final int SUFFIX_ACTION_BUTTON = 2;
    private int layoutId = R.layout.input_list_item_edit_text;
    private OnActionListener onActionListener;
    private String actionButtonText;
    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            String v = editable.toString();
            setValue(v);
        }
    };

    public ItemEditTextWithActionButtonConfig() {
        super();
    }

    public ItemEditTextWithActionButtonConfig(String key, String value, String label, String hint, String caption) {
        super(key, value, label, hint, caption);

    }

    public ItemEditTextWithActionButtonConfig(String key, String value, String label, String hint, String caption, boolean editable) {
        super(key, value, label, hint, caption, editable);

    }

    public OnActionListener getOnActionListener() {
        return onActionListener;
    }

    public void setOnActionListener(OnActionListener onActionListener) {
        this.onActionListener = onActionListener;
    }

    public String getActionButtonText() {
        return actionButtonText;
    }

    public void setActionButtonText(String actionButtonText) {
        this.actionButtonText = actionButtonText;
    }

    @Override
    public View getView(View convertView, Context mContext) {
        Holder holder = null;
        String canonicalName = ItemEditTextWithActionButtonConfig.class.getCanonicalName();
//        if (convertView != null)
//            holder = (Holder) convertView.getTag();
//        if (convertView == null || !canonicalName.equals(holder.type)) {
        holder = new Holder();

        LayoutInflater inflater = LayoutInflater.from(mContext);

        convertView = inflater.inflate(layoutId, null);
        holder.editText = (EditText) convertView.findViewById(R.id.editText);
        holder.editText.setInputType(getInputType());
        holder.editText.addTextChangedListener(textWatcher);

        holder.caption = (TextView) convertView.findViewById(R.id.caption);
        holder.label = (TextView) convertView.findViewById(R.id.label);
        holder.actionButton = (Button) convertView.findViewById(R.id.actionButton);
        holder.preActionButton = (Button) convertView.findViewById(R.id.preActionButton);
        holder.caption.setVisibility(View.GONE);
        holder.actionButton.setVisibility(View.VISIBLE);
        holder.preActionButton.setVisibility(View.VISIBLE);
        holder.preActionButton.setOnClickListener(this);
        holder.actionButton.setOnClickListener(this);
        holder.type = canonicalName;

        convertView.setTag(holder);
//        }

//        holder.actionButton.setText(getActionButtonText());
        holder.label.setText(getLabel());
        holder.editText.setSingleLine(isSingleLine());
        holder.editText.setEnabled(isEditable());
        if (getInputFilters() != null) {
            holder.editText.setFilters(getInputFilters());
        } else {
            holder.editText.setFilters(new InputFilter[]{});
        }

        holder.editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);

        if (!isSingleLine()) {
            holder.editText.setLines(4);
        } else {
            holder.editText.setLines(1);
        }
        holder.editText.setHint(getHint());
        if (StringUtils.hasText(getValue())) {
            holder.editText.setText(getValue());
        } else {
            holder.editText.setText("");
        }
        if (StringUtils.hasText(getErrorMessage())) {
            holder.editText.setError(getErrorMessage());
        } else {
            holder.editText.setError(null);
        }
        holder.caption.setText(getCaption());


        return convertView;
    }


    @Override
    public void onClick(View view) {
        int who = PREFIX_ACTION_BUTTON;
        if (view.getId() == R.id.preActionButton)
            who = PREFIX_ACTION_BUTTON;
        if (view.getId() == R.id.actionButton)
            who = SUFFIX_ACTION_BUTTON;

        if (onActionListener != null)
            onActionListener.onAction(who, this);

    }

    public interface OnActionListener {
        void onAction(int who, ItemConfig itemConfig);
    }

    class Holder {
        String type;
        TextView caption, label;
        EditText editText;
        Button actionButton;
        Button preActionButton;
    }
}
